package br.com.lumis.saas.service.consumerclient.dataprovider;

import lumis.content.table.ContentTableDataProvider;
import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularData;
import lumis.doui.table.TableSource;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.util.ITransaction;
import lumis.util.log.ILogger;
import lumis.util.log.LoggerFactory;
import br.com.lumis.saas.aws.S3StorageManager;
import br.com.lumis.saas.service.consumerclient.ConsoleController;
import br.com.lumis.saas.service.consumerclient.model.Environment;
import br.com.lumis.saas.service.consumerclient.persistence.dao.EnvironmentDAO;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;

public class S3BuckerDataProvider  extends ContentTableDataProvider {
	private EnvironmentDAO environmentDAO = new EnvironmentDAO();	
	private static final ILogger logger = LoggerFactory.getLogger(ConsoleController.class);	
	
	public S3BuckerDataProvider() throws PortalException {
		super();				
	}
		
	public void loadData(SessionConfig sessionConfig, TableSource source,
			ITransaction transaction) throws PortalException {		
		try {
			String id = (String)source.getDouiContext().getRequest().getParameter("environmentId");		
			Environment environment = environmentDAO.get(id, transaction);
			
			
			
			String nameEnviroment ;
			
			
			if(environment.getType()==2)
				nameEnviroment = "saas-" + environment.getCustomer().getSurname().toLowerCase()+"-dev";
			else
				nameEnviroment = "saas-" + environment.getCustomer().getSurname().toLowerCase()+"-db";
			
			
			S3StorageManager s3StorageManager = new S3StorageManager();
								
			AmazonS3Client s3client = s3StorageManager.getS3Client();
               
			ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(nameEnviroment);
			ObjectListing objectListing; 	                      
			
			TabularData tabularData = source.getData();
			
			do {
				objectListing = s3client.listObjects(listObjectsRequest);
				String arrNewDate = null;						
	            
				for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {	                
	            	String[] arrDate = objectSummary.getKey().split("/");
	            	
	            	if(!arrDate[1].equals(arrNewDate)){	            	
	            		ISourceData sourceData = tabularData.addRow();
	            		sourceData.put("dateBucket", arrDate[1]);
	            		sourceData.put("environmentId", environment.getId());
	            		
	            		arrNewDate = arrDate[1];
	            	}
	            }
	            listObjectsRequest.setMarker(objectListing.getNextMarker());
			} while (objectListing.isTruncated());                       
		} catch (AmazonServiceException ase) {					
			logger.error(ase);
		} catch (AmazonClientException ace) {
			logger.error(ace);
		}
	
	}

	
}
